﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PCR.Base;
using PCR.Logic;

namespace PCR
{
    public partial class FormAddEditSampleData : DevComponents.DotNetBar.Office2007Form
    {
        public IStdSampleData _sampleData;
        
        public FormAddEditSampleData(IStdSampleData sampleData)
        {
            InitializeComponent();
            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;

            if (sampleData == null)
            {
                this.Text = "添加样本数据";
            }
            else
            {
                this._sampleData = sampleData;
                this.Text = "修改样本数据";

                this.textCT.Text = sampleData.CTVal.ToString();
                this.textCellNum.Text = sampleData.CellCount.ToString();
                this.rBtnYes.Checked = sampleData.Valid;
                this.rBtnNo.Checked = !sampleData.Valid;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this._sampleData == null)
                this._sampleData = PCRRoot.Instance.CreateObject(ObjectType.StdSampleData) as IStdSampleData;
            
            double ctVal;
            if (!double.TryParse(this.textCT.Text, out ctVal))
            {
                MessageBox.Show("无效的CT数值，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textCT.SelectAll();
                this.textCT.Focus();
                return;
            }

            if (ctVal > PCRRoot.Instance.ConfigManager.MaxCTVal || ctVal < PCRRoot.Instance.ConfigManager.MinCTVal)
            {
                MessageBox.Show("CT数值过大或者过小，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textCT.SelectAll();
                this.textCT.Focus();
                return;
            }

            this._sampleData.CTVal = ctVal;

            UInt64 cellNum;
            if (!UInt64.TryParse(this.textCellNum.Text, out cellNum))
            {
                MessageBox.Show("无效的细胞数值，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textCellNum.SelectAll();
                this.textCellNum.Focus();
                return;
            }
            
            if (cellNum > PCRRoot.Instance.ConfigManager.MaxValue || cellNum < PCRRoot.Instance.ConfigManager.MinValue)
            {
                MessageBox.Show("细胞数值过大或者过小，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textCellNum.SelectAll();
                this.textCellNum.Focus();
                return;
            }

            this._sampleData.CellCount = cellNum;
            this._sampleData.Valid = this.rBtnYes.Checked;
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
